package controllers;

import java.lang.reflect.Field;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.HashMap;

import ontocompo.fractalcomponentmanager.api.ManagerItf;
import ontocompo.fractalcomponentmanager.enums.UpdateComponentType;

import play.mvc.*;


public class OntoWebApp extends Controller {
    
    public static void run(String appId) {
    	//String appId = "ontowebapp.reversestring.ReverseStringApp";
    	
    	ManagerItf ac = null;
    	
    	if (System.getSecurityManager() == null) {
            System.setSecurityManager(new SecurityManager());
        }
        try {
            String name = "Manager";
            /*Registry registry = LocateRegistry.getRegistry("mycomputer.example.com");
            ac = (AvailableComponentsItf) registry.lookup(name);*/
            
            ac = (ManagerItf) Naming.lookup("rmi://127.0.0.1/" + name);
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        String html = "";
        
        if(ac != null) {
        	
        	try {
        		ac.loadAndInitApp(appId);
	    		
				html += ac.getHTMLInterface(appId);
			} catch (RemoteException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
        } else {
        	html += "Une erreur s'est produite.";
        }
        
    	render(appId, html);
    }
    
    public static void updateHtmlTextInputComponent(String appId, String componentId, String newValue) {
    	ManagerItf ac = null;
    	
    	if (System.getSecurityManager() == null) {
            System.setSecurityManager(new SecurityManager());
        }
        try {
            String name = "Manager";
            /*Registry registry = LocateRegistry.getRegistry("mycomputer.example.com");
            ac = (AvailableComponentsItf) registry.lookup(name);*/
            
            ac = (ManagerItf) Naming.lookup("rmi://127.0.0.1/" + name);
        } catch (Exception e) {
            e.printStackTrace();
        }

        if(ac != null) {
        	try {
				ac.updateUIElement(UpdateComponentType.HTML_TEXT_INPUT, appId, componentId, newValue);		
			} catch (RemoteException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        }
        
    	run(appId);
    }
    
    public static void actionHtmlButtonComponent(String appId, String componentId) {
    	ManagerItf ac = null;
    	
    	if (System.getSecurityManager() == null) {
            System.setSecurityManager(new SecurityManager());
        }
        try {
            String name = "Manager";
            
            ac = (ManagerItf) Naming.lookup("rmi://127.0.0.1/" + name);
        } catch (Exception e) {
            e.printStackTrace();
        }

        if(ac != null) {
        	try {
				ac.triggerAction(appId, componentId);
			} catch (RemoteException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        }
        
    	run(appId);
    }

}
